package com.enjoy.xbase.sort.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.enjoy.xbase.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 描述：单选列
 * Created by cxb on 2018/8/22.
 */
public class ListScreenView implements IScreenView {
    private Context context;
    //视图id
    private int Id;
    //选择位置
    private int choicePostion;
    //数据源
    private List<String> datas = new ArrayList<>();
    private ListView contentView;
    private BaseAdapter adapter;
    private ItemClickListener itemClickListener;

    public ListScreenView(Context context, int dftChoice, List<String> datas) {
        this(context, -1, dftChoice, datas);
    }

    public ListScreenView(Context context, int Id, int dftChoice, List<String> datas) {
        this.context = context;
        this.Id = Id;
        this.choicePostion = dftChoice;
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
        init();
    }

    public void updateData(List<String> datas) {
        this.datas.clear();
        if (datas != null) {
            this.datas.addAll(datas);
        }
        this.Id = -1;
        if (adapter != null) {

            adapter.notifyDataSetChanged();
        }
    }

    /**
     * 描述初始化：创建界面
     */
    private void init() {
        ListView listView = new ListView(context);
        listView.setDivider(new ColorDrawable(Color.parseColor("#efefef")));
        listView.setDividerHeight(1);
        adapter = new ListAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                choicePostion = position;
                adapter.notifyDataSetChanged();
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(Id, choicePostion, datas.get(choicePostion));
                }
            }
        });
        this.contentView = listView;
    }


    /**
     * 描述：获取选择位置
     *
     * @return
     */
    public int getChoicePostion() {
        return choicePostion;
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public View getView() {
        return contentView;
    }

    private void fillValue(int position, ViewHolder viewHolder) {
        viewHolder.mText.setText(datas.get(position));
        if (choicePostion == position) {
            viewHolder.mText.setTextColor(Color.parseColor("#0d84e0"));
        } else {
            viewHolder.mText.setTextColor(Color.parseColor("#969696"));
        }
    }

    static class ViewHolder {
        TextView mText;

        ViewHolder(View view) {
            mText = view.findViewById(R.id.text);
        }
    }

    class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView != null) {
                viewHolder = (ViewHolder) convertView.getTag();
            } else {
                convertView = LayoutInflater.from(context).inflate(R.layout.xbase_item_select_list, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
            fillValue(position, viewHolder);
            return convertView;
        }
    }
}
